package com.tai.cobrapp.activity.crud;

import com.tai.cobrapp.R;
import com.tai.cobrapp.activity.AdminPanelActivity;
import com.tai.cobrapp.activity.helpers.MenuActivity;
import com.tai.cobrapp.database.DatabaseHelper;
import com.tai.cobrapp.model.Establecimiento;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class NewEstablishmentActivity extends MenuActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_establishment);
		
	}
	@Override
	public void onBackPressed() {
		Intent i = new Intent(NewEstablishmentActivity.this, AdminPanelActivity.class);
		startActivity(i);
		super.onBackPressed();
	}
	public void cancel_establishment_btn_pressed(View view){
		Intent i = new Intent(NewEstablishmentActivity.this, AdminPanelActivity.class);
		startActivity(i);
	}
	public void save_establishment_btn_pressed(View view){
		EditText nameEst = (EditText)findViewById(R.id.establishment_name);
		EditText description = (EditText)findViewById(R.id.establishment_description);
		//Toast.makeText(this, "Descrption:"+nameEst.getText().toString()+"FIN", Toast.LENGTH_SHORT).show();
		
		if(nameEst.getText().toString() == null || nameEst.getText().toString().isEmpty() ){
			Toast.makeText(this, this.getString(R.string.fields_incomplete) + ": " + this.getString(R.string.name), Toast.LENGTH_SHORT).show();
			return;
		}
		else{
			if(existeEst(nameEst.getText())){
				Toast.makeText(this, nameEst.getText() + " " + this.getString(R.string.establishment_already_exists), Toast.LENGTH_SHORT).show();
				nameEst.setText("");
			}
			else{
				Establecimiento establecimiento = new Establecimiento(nameEst.getText(), description.getText());
				insertEst(establecimiento);
				Toast.makeText(this, this.getString(R.string.establishment_creation_successful) + ": " + nameEst.getText(), Toast.LENGTH_SHORT).show();
				nameEst.setText("");
				description.setText("");
			}
		}
	}
	public boolean existeEst(Editable establishment){
		//return false;
		if(DatabaseHelper.getDatabaseInstance(this).existeEstablecimiento(establishment)){
			return true;
		}else{
			return false;
		}
	}
	public void insertEst(Establecimiento est){
		DatabaseHelper.getDatabaseInstance(this).insertarEstablecimiento(est);
	}
}
